﻿using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class EmpInfo : System.Web.UI.Page
    {
        protected Employee employee;
        protected string empDeptName;
        protected IQueryable<EmpDepPosition> empDepPositions;
        protected IQueryable<EmpWork> empWorks;
        protected IQueryable<EmpEducation> empEducations;
        protected IQueryable<EmpFamily> empFamilies;
        protected IQueryable<EmpTrain> empTrains;
        protected IQueryable<EmpAward> empAwards;
        protected IQueryable<EmpContact> empContacts;
        protected IQueryable<EmpExam> empExams;
        protected IQueryable<EmpPromotion> empPromotions;
        ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);

        protected void Page_Load(object sender, System.EventArgs e)
        {
            int empId = int.Parse(Request.QueryString["EmpId"]);
            employee = ctx.Employees.Single(r => r.Id == empId);
            empDepPositions = ctx.EmpDepPositions.Where(r => r.EmpId == empId).OrderBy(r=>r.PosId);
            empDeptName = ctx.Depts.Single(r => r.Id == empDepPositions.First().DeptId).Text;
            empWorks = ctx.EmpWorks.Where(r => r.EmpId == empId);
            empEducations = ctx.EmpEducations.Where(r => r.EmpId == empId);
            empFamilies = ctx.EmpFamilies.Where(r => r.EmpId == empId);
            empTrains = ctx.EmpTrains.Where(r => r.EmpId == empId);
            empAwards = ctx.EmpAwards.Where(r => r.EmpId == empId);
            empContacts = ctx.EmpContacts.Where(r => r.EmpId == empId);
            empExams = ctx.EmpExams.Where(r => r.EmpId == empId);
            empPromotions = ctx.EmpPromotions.Where(r => r.EmpId == empId);

            ImageData.Src = "Photoes\\" + employee.PhotoName;
            rptEdu.DataSource = empEducations;
            rptEdu.DataBind();
            rptWork.DataSource = empWorks;
            rptWork.DataBind();
            rptFamily.DataSource = empFamilies;
            rptFamily.DataBind();
            rptCriminal.DataSource = empAwards;
            rptCriminal.DataBind();
            rptTrain.DataSource = empTrains;
            rptTrain.DataBind();
            rptContract.DataSource = empContacts;
            rptContract.DataBind();
            rptExam.DataSource = empExams;
            rptExam.DataBind();
            rptPromotion.DataSource = empPromotions;
            rptPromotion.DataBind();
        }

        protected string getDeptName(object id)
        {
            var v = ctx.Depts.Single(r => r.Id ==int.Parse(id.ToString()));
            return v.Text;
        }

        protected string getPositionName(object id)
        {
            var v = ctx.Positions.Single(r => r.Id == int.Parse(id.ToString()));
            return v.Name;
        }
    }
}